//
//  RecentBaseCell.m
//  SportSocial
//
//  Created by yull on 15/1/28.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "RecentBaseCell.h"
#import <UIImageView+WebCache.h>

@implementation RecentBaseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        CGRect rect= CGRectMake(10, 10, 50, 50);
        self.headImageView = [[UIImageView alloc] initWithFrame:rect];
        self.headImageView.layer.cornerRadius = self.headImageView.width/2;
        self.headImageView.layer.masksToBounds = YES;
        self.headImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapAvatar:)];
        [self.headImageView addGestureRecognizer:tapGesture];
        [self.contentView addSubview:self.headImageView];
        
        rect = CGRectMake(self.headImageView.right + self.headImageView.left, 10, 0, 25);
        self.nameLabel = [[UILabel alloc] initWithFrame:rect];
        self.nameLabel.backgroundColor = [UIColor clearColor];
        self.nameLabel.font = [UIFont boldSystemFontOfSize:20.0f];
        self.nameLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:self.nameLabel];
        
        rect = CGRectMake(self.nameLabel.left, self.nameLabel.bottom+5, self.contentView.width - self.nameLabel.left - 10, self.nameLabel.height);
        self.contentLabel = [[UILabel alloc] initWithFrame:rect];
        self.contentLabel.backgroundColor = [UIColor clearColor];
        self.contentLabel.font = [UIFont systemFontOfSize:14.0f];
        self.contentLabel.textColor = RGB(130.0f, 130.0f, 130.0f);
        self.contentLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.contentLabel];
        
        rect = CGRectMake(self.contentView.width-120, self.nameLabel.top, 110, self.nameLabel.height);
        self.timeLabel = [[UILabel alloc] initWithFrame:rect];
        self.timeLabel.backgroundColor = [UIColor clearColor];
        self.timeLabel.textAlignment = NSTextAlignmentRight;
        self.timeLabel.font = [UIFont systemFontOfSize:12.0f];
        self.timeLabel.textColor = RGB(130.0f, 130.0f, 130.0f);
        self.timeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [self.contentView addSubview:self.timeLabel];
    }
    return self;
}

+ (CGFloat)defaultCellHeight
{
    return 75.0f;
}

- (void)drawCellWithRecent:(ChatInfo *)recent
{
    self.chatInfo = recent;
    
    UIImage *placeholderImage = recent.infoType == ChatInfoTypeGroup ? [UIImage imageNamed:@"qun"] : [UIImage imageNamed:@"zc_photo_bg"];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:recent.icon] placeholderImage:placeholderImage];
    
    self.nameLabel.text = recent.name;
    
    self.contentLabel.text = recent.content;
    
    self.timeLabel.text = [FunctionMethod getChatTimeTextWithTimeStamp:recent.time];;
    
    CGFloat nameWidth = [self.nameLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, self.nameLabel.height)].width;
    self.nameLabel.width = nameWidth;
}

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsMake(0, self.nameLabel.left, 0, 0);
}

#pragma mark -
- (void)onTapAvatar:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(onClickRecentAvatarView:)]) {
        [self.delegate onClickRecentAvatarView:self.chatInfo];
    }
}

@end
